package com.wanghaixin.controller;

import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarMake;
import com.wanghaixin.pojo.CarManufacturer;
import com.wanghaixin.pojo.CarParamType;
import com.wanghaixin.service.CarMakeService;
import com.wanghaixin.service.CarParamTypeService;
import com.wanghaixin.service.CarmanuFacturerService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 17:26
 * @Description: 厂商管理表现层
 */
@Controller
public class CarManufacturerController {
    @Autowired
    private CarmanuFacturerService carmanuFacturerService;

    //通过页面上所属品牌，可知这里需要品牌的service层
    @Autowired
    private CarMakeService carMakeService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:27  2020/11/2
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/manufacturer/list")
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        DataGridResult byPage = carmanuFacturerService.findByPage(queryDTO);
        return byPage;
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:27  2020/11/2
     * @Version:  1.0.0
     * @Description: 删除
     */
    @RequestMapping("/sys/manufacturer/del")
    @ResponseBody
    public R delCarManufacturer(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
        int i = carmanuFacturerService.delCarmanuFacturer(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:27  2020/11/2
     * @Version:  1.0.0
     * @Description: 添加时，获取分类信息的能力
     */
    @RequestMapping("/sys/manufacturer/typeall")
    @ResponseBody
    public R typeall(){
        List<CarMake> all = carMakeService.findAll();
        return R.ok().put("sites",all);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:27  2020/11/2
     * @Version:  1.0.0
     * @Description: id查询，记得将结果返回
     */
    @RequestMapping("/sys/manufacturer/info/{id}")
    @ResponseBody
    public R upCarManufacturer(@PathVariable("id") Integer id){
        CarManufacturer byId = carmanuFacturerService.findById(id);
        return R.ok().put("manufacturer",byId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:27  2020/11/2
     * @Version:  1.0.0
     * @Description: 添加 添加参数注解
     */
    @RequestMapping("/sys/manufacturer/save")
    @ResponseBody
    public R addCarManufacturer(@RequestBody CarManufacturer carManufacturer){
        int i = carmanuFacturerService.addCarmanuFacturer(carManufacturer);
        return i>0 ? R.ok(): R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  17:43  2020/11/2
     * @Version:  1.0.0
     * @Description:  修改
     */
    @RequestMapping("/sys/manufacturer/update")
    @ResponseBody
    public R upCarManufacturer(@RequestBody CarManufacturer carManufacturer){
        int i = carmanuFacturerService.upCarmanuFacturer(carManufacturer);
        return i>0 ? R.ok() : R.error("修改失败");
    }
}
